﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OcrAnswerQuestionsForm
{
    public partial class OptionForm : AntdUI.Window
    {
        public OptionForm()
        {
            InitializeComponent();
            //AutoDpi(this);
        }

        private void OptionForm_Load(object sender, EventArgs e)
        {
            // 初始化设置数据
            var config1 = ConfigSettings.ReadSetting("MFontSize");
            var config2 = ConfigSettings.ReadSetting("FFontSize");
            var config3 = ConfigSettings.ReadSetting("MFontPointX");
            var config4 = ConfigSettings.ReadSetting("MFontPointY");
            //var config5 = ConfigSettings.ReadSetting("FFontPointX");
            //var config6 = ConfigSettings.ReadSetting("FFontPointY");
            var config7 = ConfigSettings.ReadSetting("ShowA");
            var config8 = ConfigSettings.ReadSetting("ShowB");
            txtMFontSize.Text = config1;
            txtFFontSize.Text = config2;
            txtMFontPointX.Text = config3;
            txtMFontPointY.Text = config4;
            //txtFFontPointX.Text = config5;
            //txtFFontPointY.Text = config6;
            checkBox1.Checked = (config7 == true.ToString());
            checkBox2.Checked = (config8 == true.ToString());

            checkBox2.Checked = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            // write a new value for the Test1 setting
            ConfigSettings.WriteSetting("MFontSize", txtMFontSize.Text.Trim());
            ConfigSettings.WriteSetting("FFontSize", txtFFontSize.Text.Trim());
            ConfigSettings.WriteSetting("MFontPointX", txtMFontPointX.Text.Trim());
            ConfigSettings.WriteSetting("MFontPointY", txtMFontPointY.Text.Trim());
            //ConfigSettings.WriteSetting("FFontPointX", txtFFontPointX.Text.Trim());
            //ConfigSettings.WriteSetting("FFontPointY", txtFFontPointY.Text.Trim());
            ConfigSettings.WriteSetting("ShowA", checkBox1.Checked.ToString());
            ConfigSettings.WriteSetting("ShowB", checkBox2.Checked.ToString());
            MessageBox.Show(@"保存完成");

            var messageForm = WuXiaOcrAQ.MessageForm.CreateForm();
            //messageForm.Close();
            SafeCloseForm(messageForm);
            //if (messageForm.InvokeRequired)
            //{
            //    messageForm.Invoke(new MethodInvoker(delegate
            //    {
            //        messageForm.Show();
            //    }));
            //}
            //else
            //{
            //    messageForm.Show();
            //}

            this.Close();
        }

        public void SafeCloseForm(Form form)
        {
            if (form == null || form.IsDisposed || form.Disposing)
                return;

            if (form.InvokeRequired)
            {
                try
                {
                    form.BeginInvoke(new MethodInvoker(() =>
                    {
                        if (!form.IsDisposed && !form.Disposing)
                        {
                            form.Close();
                        }
                    }));
                }
                catch (Exception ex) when (ex is System.ComponentModel.InvalidAsynchronousStateException ||
                                           ex is System.Threading.ThreadStateException)
                {
                    Console.WriteLine("无法安全关闭窗体：" + ex.Message);
                }
            }
            else
            {
                form.Close();
            }
        }
    }
}
